<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class jQuickForm_Js{

    public $js=array();
    public $js_inline=array();
    public $js_onload=array();
    public $js_loadFCK=array();


    function addJs($js){
        $this->js[$js] = $js;
    }

    function addJsInline($js, $id=null){
        if($id){
            $this->js_inline[$id] = $js;
        } else {
            $this->js_inline[] = $js;
        }
    }
    function addJsFCK($js, $id=null){
        if($id){
            $this->js_loadFCK[$id] = $js;
        } else {
            $this->js_loadFCK[] = $js;
        }
    }
    function addJsOnload($js, $id=null){
        $this->needJquery();
        if($id){
            $this->js_onload[$id] = $js;
        } else {
            $this->js_onload[] = $js;
        }
//        debug($this->js_onload);
    }

    function getJsAll(){
//        jaguar_debug($this->js);
		$this->needJquery();
		$this->needJqueryUi();
		$this->addJs('/data/js/jquickform/quickform.js');
        return $this->getJs()."\n".
        $this->getJsInline()."\n".
        $this->getJsOnload();
    }
    function getJs(){
        if(!count($this->js)) return '';
        $js_code = '';
        foreach ($this->js as $js) {
        	$js_code .= "\t".'<script language="JavaScript" type="text/javascript" '."".'src="'.$js.'"></script>'."\n";
        }
        return $js_code;
    }
    function getJsInline(){
        if(!count($this->js_inline)) return '';
        $js_code = '<script language="JavaScript" type="text/javascript">';
        foreach ($this->js_inline as $js) {
            //если требуется сжатие
            if(jQuickForm::config()->getJsMin()){
                $js = JSMin::minify($js);
            }
        	$js_code .= $js."\n";
        }
		$js_code .= $this->getJsFCK();
        return $js_code.'</script>';
    }
    function getJsFCK(){
        if(!count($this->js_loadFCK)) return '';
        $js_code = "\r\nfunction loadFCK() {";
        foreach ($this->js_loadFCK as $js) {
        	$js_code .= $js."\n";
        }
        return $js_code."}\r\n";
    }
    function getJsOnload(){
        if(!count($this->js_onload)) return '';
        $js = implode("\n",$this->js_onload);
        //если требуется сжатие
        if(jQuickForm::config()->getJsMin()){
            $js = JSMin::minify($js);
        }
        return  '<script language="JavaScript" type="text/javascript">
    jQuery(document).ready(
        function(){'."\n".$js."\n".'}
    );'."\n".'</script>';
    }

    function needJquery(){
        //$this->addJs('/jquery.min.js');
    }
    function needJqueryMask(){
        $this->needJquery();
        $this->addJs('/vendors/jquery/jquery.maskedinput-1.2.2.js');
    }
    function needJqueryUi(){
        $this->needJquery();
        //jQuickForm::js()->addJs('/vendors/jquery/ui/jquery-ui.min.js');
        //jQuickForm::js()->addJs('/vendors/jquery/ui/jquery.tools.min.js');
        //jQuickForm::css()->addCss('/vendors/jquery/ui/css/'.jQuickForm::config()->getUiTheme().'/jquery-ui.css');
    }
}
?>